
{***********************************************************}
{                                                           }
{      Luxena Database Library                              }
{                                                           }
{      Common Database Types                                }
{                                                           }
{      Copyright (c) 2001-2008 Luxena Software Company      }
{      http://www.luxena.com                                }
{      e-mail: support@luxena.com                           }
{                                                           }
{***********************************************************}

unit LXDBTypes;

{$I lx.inc}

interface

{$IFDEF D6}
uses
  DB, FMTBcd, SqlTimSt;
type
  PBcd = FMTBcd.PBcd;
  TBcd = FMTBcd.TBcd;
  PSQLTimeStamp = SqlTimSt.PSQLTimeStamp;
  TSQLTimeStamp = SqlTimSt.TSQLTimeStamp;
{$ELSE}
uses
  SysUtils, DB, LXVariants;
type
  PBcd = DB.PBcd;
  TBcd = DB.TBcd;
  PSQLTimeStamp = ^TSQLTimeStamp;
  TSQLTimeStamp = packed record
    Year: SmallInt;
    Month: Word;
    Day: Word;
    Hour: Word;
    Minute: Word;
    Second: Word;
    Fractions: LongWord;
  end;
{$ENDIF}

const LXFieldTypeVarMap: array [TFieldType] of TVarType = (
  varEmpty,         // ftUnknown
  varString,        // ftString,
  varSmallint,      // ftSmallint,
  varInteger,       // ftInteger
  varWord,          // ftWord
  varBoolean,       // ftBoolean
  varDouble,        // ftFloat
  varCurrency,      // ftCurrency
  varCurrency,      // ftBCD
  varDate,          // ftDate
  varDate,          // ftTime
  varDate,          // ftDateTime
  varString,        // ftBytes
  varString,        // ftVarBytes
  varInteger,       // ftAutoInc
  varString,        // ftBlob
  varString,        // ftMemo
  varString,        // ftGraphic
  varString,        // ftFmtMemo
  varString,        // ftParadoxOle
  varString,        // ftDBaseOle
  varString,        // ftTypedBinary
  varEmpty,         // ftCursor
  varString,        // ftFixedChar
  varOleStr,        // ftWideString
  varInt64,         // ftLargeint
  varEmpty,         // ftADT
  varEmpty,         // ftArray
  varEmpty,         // ftReference
  varEmpty,         // ftDataSet
  varString,        // ftOraBlob
  varString,        // ftOraClob
  varVariant,       // ftVariant
  varUnknown,       // ftInterface
  varDispatch,      // ftIDispatch
  varString         // ftGuid
{$IFDEF D6},
  varDate,          // ftTimeStamp
  varCurrency       // ftFMTBcd
{$ENDIF}
{$IFDEF D10},
  varOleStr,        // ftFixedWideChar
  varOleStr,        // ftWideMemo
  varEmpty,         // ftOraTimeStamp
  varString         // ftOraIntervals
{$ENDIF}
,varString
,varString
,varString
,varString
,varString
,varString
,varString
);


function LXBcdToStr(const Value: TBcd): string;
function LXBcdToInteger(const Value: TBcd): Integer;
function LXBcdToDouble(const Value: TBcd): Double;
function LXBcdToVar(const Value: TBcd): Variant;

procedure LXStrToBcd(const Value: string; var Bcd: TBcd);
procedure LXIntegerToBcd(Value: Integer; var Bcd: TBcd);
procedure LXDoubleToBcd(Value: Double; var Bcd: TBcd);
function LXCurrToBcd(Curr: Currency; var Bcd: TBcd; Precision: Integer = 32;
  Decimals: Integer = 4): Boolean;
function LXVarToBcd(const Value: Variant): TBcd;


implementation

function LXBcdToStr(const Value: TBcd): string;
{$IFDEF D6}
begin
  Result := BcdToStr(Value);
{$ELSE}
var
  Curr: Currency;
begin
  BCDToCurr(Value, Curr);
  Result := CurrToStr(Curr);
{$ENDIF}
end;

function LXBcdToInteger(const Value: TBcd): Integer;
{$IFDEF D6}
begin
  Result := BcdToInteger(Value);
{$ELSE}
var
  Curr: Currency;
begin
  BCDToCurr(Value, Curr);
  Result := Round(Curr);
{$ENDIF}
end;

function LXBcdToDouble(const Value: TBcd): Double;
{$IFDEF D6}
begin
  Result := BcdToDouble(Value);
{$ELSE}
var
  Curr: Currency;
begin
  BCDToCurr(Value, Curr);
  Result := Curr;
{$ENDIF}
end;

function LXBcdToVar(const Value: TBcd): Variant;
{$IFDEF D6}
begin
  Result := VarFMTBcdCreate(Value);
{$ELSE}
var
  Curr: Currency;
begin
  BCDToCurr(Value, Curr);
  Result := Curr;
{$ENDIF}
end;

procedure LXStrToBcd(const Value: string; var Bcd: TBcd);
begin
{$IFDEF D6}
  Bcd := StrToBcd(Value);
{$ELSE}
  CurrToBcd(StrToCurr(Value), Bcd);
{$ENDIF}
end;

procedure LXIntegerToBcd(Value: Integer; var Bcd: TBcd);
begin
{$IFDEF D6}
  Bcd := IntegerToBcd(Value);
{$ELSE}
  CurrToBcd(Value, Bcd);
{$ENDIF}
end;

procedure LXDoubleToBcd(Value: Double; var Bcd: TBcd);
begin
{$IFDEF D6}
  DoubleToBcd(Value, Bcd);
{$ELSE}
  CurrToBcd(Value, Bcd);
{$ENDIF}
end;

function LXCurrToBcd(Curr: Currency; var Bcd: TBcd; Precision: Integer;
  Decimals: Integer): Boolean;
begin
  Result := CurrToBcd(Curr, Bcd, Precision, Decimals);
end;

function LXVarToBcd(const Value: Variant): TBcd;
begin
{$IFDEF D6}
  Result := VarToBcd(Value);
{$ELSE}
  CurrToBcd(Value, Result);
{$ENDIF}
end;


initialization
{$IFDEF D6}
  LXFieldTypeVarMap[ftTimeStamp] := varSQLTimeStamp;
  LXFieldTypeVarMap[ftFMTBcd] := varFMTBcd;
{$ENDIF}

end.